package com.atguigu.msmservice.utils;

import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * @Author: shaki
 * @Date: 2021/6/22 11:26
 */
@Component
public class MailUtils {
    @Autowired
    private JavaMailSender mailSender;//一定要用@Autowired

    //application.properties中已配置的值
    @Value("${spring.mail.username}")
    private String from;

    //有时@Value不生效 直接把from写死
    //public String from = "你的邮箱";//填写你自己的邮箱


    public boolean sendMimeMail( String subject, String text, String to) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject(subject);//主题
        mailMessage.setText(text);//内容
        mailMessage.setTo(to);//发给谁
        if(StringUtils.isNullOrEmpty(from)){
            from = "1641717914@qq.com";
        }
        mailMessage.setFrom(from);//你自己的邮箱
        try {
            mailSender.send(mailMessage);//发送
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
